package Controller

import (
	"github.com/gin-gonic/gin"
	"selfService/ginWeb/Service"
	"selfService/ginWeb/Service/pbfiles"
	"selfService/tool"
)
var PersonService Service.PersonService

// 注册用户
func Register(c *gin.Context)  {
	request := &service.CreateUserByPhoneRequest{}
	request.Phone = c.PostForm("phone")
	request.Password = c.PostForm("password")
	user, err := PersonService.CreateUser(request)
	println(user)
	if err != nil{
		tool.Failed(c,"失败")
		return
	}
	if user.Message == "创建失败，手机号已存在" {
		tool.Failed(c,"创建失败，手机号已存在")
	}else {
		tool.Success(c,"添加成功")
	}
}

//rpc get_login(loginin_request) returns (login_response);    // 传入phone和password 返回信息
func GetLogin(c *gin.Context)  {
	request := &service.LogininRequest{}
	request.Phone = c.Query("phone")
	request.Password = c.Query("password")
	user, err := PersonService.GetLogin(request)
	println(user)
	if err != nil{
		tool.Failed(c,"失败")
		return
	}
	tool.Success(c,user)
}

//rpc get_indi_authority(Indi_authority_request) returns(Indi_authority_response); //传入id 查询个人权限列表
func GetIndiAuthority(c *gin.Context)  {
	request := &service.IndiAuthorityRequest{}
	request.Id = c.Query("id")
	user, err := PersonService.GetIndiAuthority(request)
	println(user)
	if err != nil{
		tool.Failed(c,"失败")
		return
	}
	tool.Success(c,user)
}

//rpc Get_stuInfo(Stu_info_request) returns (Stu_info_response); // 传入id 查询学生信息
func GetStuInfo(c *gin.Context)  {
	request := &service.StuInfoRequest{}
	request.Id = c.Query("id")
	user, err := PersonService.GetStuInfo(request)
	println(user)
	if err != nil{
		tool.Failed(c,"失败")
		return
	}
	tool.Success(c,user)
}

//rpc Get_userInfo(GetUserInfoByid_request) returns(login_response);//传入id 返回个人信息
func GetUserInfo(c *gin.Context)  {
	request := &service.GetUserInfoByidRequest{}
	request.Id = c.Query("id")
	user, err := PersonService.GetUserInfo(request)
	println(user)
	if err != nil{
		tool.Failed(c,"失败")
		return
	}
	tool.Success(c,user)
}

//rpc UpdateUserInfo(UpdateUserInfo_request) returns(UpdateUserInfo_response); // 修改用户资料
func UpdateUserInfo(c *gin.Context)  {
	request := &service.UpdateUserInfoRequest{}
	request.Id = c.PostForm("id")
	request.NickName = c.PostForm("nickName")
	request.Gender = c.PostForm("gender")
	request.Idiograph = c.PostForm("idiograph")
	request.Telephone = c.PostForm("telephone")
	request.Age = c.PostForm("age")
	request.HeadshotUrl = c.PostForm("headShotUrl")
	request.Password = c.PostForm("password")
	user, err := PersonService.UpdateUserInfo(request)
	if err != nil{
		tool.Failed(c,"失败")
		return
	}
	tool.Success(c,user.Message)
}